%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% File:               ReplicateTable4.m
%
% Author:             Tasos Kalandrakis
%
% Description:        Script that fully replicates Table 4
%
% Dependencies:       Loads 'GERN.mat' and calls 'UCombMAX.m'
%
% Created:            Sep - 2021
%
% Last Modified:      Feb - 2022
%
% Language:           MATLAB
%
% Related References: [1] Kalandrakis, Tasos. 2022. "One-dimensional
%                     scaling without apologies," Journal of Politics,
%                     forthcoming.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
filedir=pwd;

Surveys=strcat(filedir,'/GERN.mat');
load(Surveys)

years = unique(cellfun(@(x) x(2:end),fieldnames(GERN),'UniformOutput', false));
for kk=1:6
    Dname = strcat('M',years{kk});
    D = GERN.(Dname);
    Pname = strcat('P',years{kk});
    P = GERN.(Pname);
    
    Pind=1:length(P);
    
    PL = Pind(matches(P,'SPD'));
    PR = Pind(matches(P,'CDU'));
    if Pind(matches(P,'CSU'))>6
        the6=[1 2 7 4 5 6];
    else
        the6=1:6;
    end
    
    if length(P)>6
        %Full
        [OB,OR,ORU,Orders,Est] = UCombMAX(D,PL,PR,0.05,1000);
        Fname = strcat(Dname,'F');
        save(Fname,'OB','OR','ORU','Orders','Est')
        %Six
        [OB,OR,ORU,Orders,Est] = UCombMAX(D(:,the6),PL,PR,0.05,1000);
        Fname = strcat(Dname,'6');
        save(Fname,'OB','OR','ORU','Orders','Est')
    else
        %Six is Full
        [OB,OR,ORU,Orders,Est] = UCombMAX(D,PL,PR,0.05,1000);
        Fname = strcat(Dname,'F');
        save(Fname,'OB','OR','ORU','Orders','Est')
        Fname = strcat(Dname,'6');
        save(Fname,'OB','OR','ORU','Orders','Est')
    end
end


Table4=cell(17,7);

i=1;
Table4{2,i}='pi-hat';
Table4{3,i}='Score';
Table4{4,i}='(as %)';
Table4{5,i}='Strong test';
Table4{6,i}='Weak test';
Table4{7,i}='Joint weak test';
Table4{8,i}='J';
Table4{9,i}='N';
Table4{10,i}='pi-hat';
Table4{11,i}='Score';
Table4{12,i}='(as %)';
Table4{13,i}='Strong test';
Table4{14,i}='Weak test';
Table4{15,i}='Joint weak test';
Table4{16,i}='J';
Table4{17,i}='N';

Table4{1,2}=1998;
Table4{1,3}=2002;
Table4{1,4}=2005;
Table4{1,5}=2009;
Table4{1,6}=2013;
Table4{1,7}=2017;

i=i+1;
load(strcat(filedir,'/M986.mat'))
Table4{8,i}=Est.P;
Table4{9,i}=Est.N;
Table4{2,i}=GERN.P98(Est.Qorder);
Table4{3,i}=Est.Qsc;
Table4{4,i}=Est.Qsc/(nchoosek(Est.P,3)*Est.N);
Table4{5,i}=GERN.P98(Est.Orders(OB.reject==0,:));
Table4{6,i}=GERN.P98(Est.Orders(OR.reject==0,:));
Table4{7,i}=GERN.P98(Est.Orders(sum(ORU.reject,2)<Table4{8,i},:));

load(strcat(filedir,'/M98F.mat'))
Table4{16,i}=Est.P;
Table4{17,i}=Est.N;
Table4{10,i}=GERN.P98(Est.Qorder);
Table4{11,i}=Est.Qsc;
Table4{12,i}=Est.Qsc/(nchoosek(Est.P,3)*Est.N);
Table4{13,i}=GERN.P98(Est.Orders(OB.reject==0,:));
Table4{14,i}=GERN.P98(Est.Orders(OR.reject==0,:));
Table4{15,i}=GERN.P98(Est.Orders(sum(ORU.reject,2)<Table4{16,i},:));

i=i+1;
load(strcat(filedir,'/Ma026.mat'))
Table4{8,i}=Est.P;
Table4{9,i}=Est.N;
Table4{2,i}=GERN.Pa02(Est.Qorder);
Table4{3,i}=Est.Qsc;
Table4{4,i}=Est.Qsc/(nchoosek(Est.P,3)*Est.N);
Table4{5,i}=GERN.Pa02(Est.Orders(OB.reject==0,:));
Table4{6,i}=GERN.Pa02(Est.Orders(OR.reject==0,:));
Table4{7,i}=GERN.Pa02(Est.Orders(sum(ORU.reject,2)<Table4{8,i},:));

load(strcat(filedir,'/Ma02F.mat'))
Table4{16,i}=Est.P;
Table4{17,i}=Est.N;
Table4{10,i}=GERN.Pa02(Est.Qorder);
Table4{11,i}=Est.Qsc;
Table4{12,i}=Est.Qsc/(nchoosek(Est.P,3)*Est.N);
Table4{13,i}=GERN.Pa02(Est.Orders(OB.reject==0,:));
Table4{14,i}=GERN.Pa02(Est.Orders(OR.reject==0,:));
Table4{15,i}=GERN.Pa02(Est.Orders(sum(ORU.reject,2)<Table4{16,i},:));

i=i+1;
load(strcat(filedir,'/M056.mat'))
Table4{8,i}=Est.P;
Table4{9,i}=Est.N;
Table4{2,i}=GERN.P05(Est.Qorder);
Table4{3,i}=Est.Qsc;
Table4{4,i}=Est.Qsc/(nchoosek(Est.P,3)*Est.N);
Table4{5,i}=GERN.P05(Est.Orders(OB.reject==0,:));
Table4{6,i}=GERN.P05(Est.Orders(OR.reject==0,:));
Table4{7,i}=GERN.P05(Est.Orders(sum(ORU.reject,2)<Table4{8,i},:));

load(strcat(filedir,'/M05F.mat'))
Table4{16,i}=Est.P;
Table4{17,i}=Est.N;
Table4{10,i}=GERN.P05(Est.Qorder);
Table4{11,i}=Est.Qsc;
Table4{12,i}=Est.Qsc/(nchoosek(Est.P,3)*Est.N);
Table4{13,i}=GERN.P05(Est.Orders(OB.reject==0,:));
Table4{14,i}=GERN.P05(Est.Orders(OR.reject==0,:));
Table4{15,i}=GERN.P05(Est.Orders(sum(ORU.reject,2)<Table4{16,i},:));

i=i+1;
load(strcat(filedir,'/M096.mat'))
Table4{8,i}=Est.P;
Table4{9,i}=Est.N;
Table4{2,i}=GERN.P09(Est.Qorder);
Table4{3,i}=Est.Qsc;
Table4{4,i}=Est.Qsc/(nchoosek(Est.P,3)*Est.N);
Table4{5,i}=GERN.P09(Est.Orders(OB.reject==0,:));
Table4{6,i}=GERN.P09(Est.Orders(OR.reject==0,:));
Table4{7,i}=GERN.P09(Est.Orders(sum(ORU.reject,2)<Table4{8,i},:));

load(strcat(filedir,'/M09F.mat'))
Table4{16,i}=Est.P;
Table4{17,i}=Est.N;
Table4{10,i}=GERN.P09(Est.Qorder);
Table4{11,i}=Est.Qsc;
Table4{12,i}=Est.Qsc/(nchoosek(Est.P,3)*Est.N);
Table4{13,i}=GERN.P09(Est.Orders(OB.reject==0,:));
Table4{14,i}=GERN.P09(Est.Orders(OR.reject==0,:));
Table4{15,i}=GERN.P05(Est.Orders(sum(ORU.reject,2)<Table4{16,i},:));

i=i+1;
load(strcat(filedir,'/M136.mat'))
Table4{8,i}=Est.P;
Table4{9,i}=Est.N;
Table4{2,i}=GERN.P13(Est.Qorder);
Table4{3,i}=Est.Qsc;
Table4{4,i}=Est.Qsc/(nchoosek(Est.P,3)*Est.N);
Table4{5,i}=GERN.P13(Est.Orders(OB.reject==0,:));
Table4{6,i}=GERN.P13(Est.Orders(OR.reject==0,:));
Table4{7,i}=GERN.P13(Est.Orders(sum(ORU.reject,2)<Table4{8,i},:));

load(strcat(filedir,'/M13F.mat'))
Table4{16,i}=Est.P;
Table4{17,i}=Est.N;
Table4{10,i}=GERN.P13(Est.Qorder);
Table4{11,i}=Est.Qsc;
Table4{12,i}=Est.Qsc/(nchoosek(Est.P,3)*Est.N);
Table4{13,i}=GERN.P13(Est.Orders(OB.reject==0,:));
Table4{14,i}=GERN.P13(Est.Orders(OR.reject==0,:));
Table4{15,i}=GERN.P13(Est.Orders(sum(ORU.reject,2)<Table4{16,i},:));

i=i+1;
load(strcat(filedir,'/M176.mat'))
Table4{8,i}=Est.P;
Table4{9,i}=Est.N;
Table4{2,i}=GERN.P17(Est.Qorder);
Table4{3,i}=Est.Qsc;
Table4{4,i}=Est.Qsc/(nchoosek(Est.P,3)*Est.N);
Table4{5,i}=GERN.P17(Est.Orders(OB.reject==0,:));
Table4{6,i}=GERN.P17(Est.Orders(OR.reject==0,:));
Table4{7,i}=GERN.P17(Est.Orders(sum(ORU.reject,2)<Table4{8,i},:));

load(strcat(filedir,'/M17F.mat'))
Table4{16,i}=Est.P;
Table4{17,i}=Est.N;
Table4{10,i}=GERN.P17(Est.Qorder);
Table4{11,i}=Est.Qsc;
Table4{12,i}=Est.Qsc/(nchoosek(Est.P,3)*Est.N);
Table4{13,i}=GERN.P17(Est.Orders(OB.reject==0,:));
Table4{14,i}=GERN.P17(Est.Orders(OR.reject==0,:));
Table4{15,i}=GERN.P17(Est.Orders(sum(ORU.reject,2)<Table4{16,i},:));


openvar('Table4')
